package com.bringway.p2p.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bringway.fm.controller.AttController;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.global.service.SysAttechmentService;
import com.bringway.p2p.service.POStzrService;

/*
* @author 作者 hc
* @version 创建时间：2016年2月1日 上午10:46:55
* 类说明:业务功能-附件管理
*/

@Controller
@RequestMapping("/POStzr")
public class POStzrController extends JsonController {

	@Autowired
	private POStzrService postzrService;

	@RequestMapping("/queryPostzrList")
	public ModelAndView queryPostzrList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();

		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String sh_start_date = RequestUtils.getStringParameter(request, "sh_start_date");
		String sh_end_date = RequestUtils.getStringParameter(request, "sh_end_date");
		String zq_id = RequestUtils.getStringParameter(request, "zq_id");
		String pos_code = RequestUtils.getStringParameter(request, "pos_code");
		String pos_owner_user_id = RequestUtils.getStringParameter(request, "pos_owner_user_id");
		String sh_state = RequestUtils.getStringParameter(request, "sh_state");

		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("sh_start_date", sh_start_date);
		params.put("sh_end_date", sh_end_date);
		params.put("zq_id", zq_id);
		params.put("pos_code", pos_code);
		params.put("pos_owner_user_id", pos_owner_user_id);
		params.put("sh_state", sh_state);

		List list = postzrService.queryPostzrList(params);
		int count = postzrService.getPostzrCount(params);
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}

	@RequestMapping("exportTxExcelMethod")
	public void exportAttExcelMethod2(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String sh_start_date = RequestUtils.getStringParameter(request, "sh_start_date");
		String sh_end_date = RequestUtils.getStringParameter(request, "sh_end_date");
		String zq_id = RequestUtils.getStringParameter(request, "zq_id");
		String pos_code = RequestUtils.getStringParameter(request, "pos_code");
		String pos_owner_user_id = RequestUtils.getStringParameter(request, "pos_owner_user_id");
		String sh_state = RequestUtils.getStringParameter(request, "sh_state");

		JSONObject params = new JSONObject();

		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("sh_start_date", sh_start_date);
		params.put("sh_end_date", sh_end_date);
		params.put("zq_id", zq_id);
		params.put("pos_code", pos_code);
		params.put("pos_owner_user_id", pos_owner_user_id);
		params.put("sh_state", sh_state);
		// 导出页面数据
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		List<Map<String, Object>> posList = this.postzrService.queryPostzrList(params);

		sheetList = this.postzrService.exportPosData(posList);
		// 带Format的Excel导出
		ExcelOperUtil util = new ExcelOperUtil();
		// 获取template相对路径cn.com.worklog.
		InputStream stream = AttController.class.getResourceAsStream("/com/bringway/p2p/config/pos.xls");
		OutputStream os = response.getOutputStream();
		super.setDownLoadResponseHeader(request, response, "pos单投资人信息.xls");
		util.writeToExcelByStencil(stream, sheetList, os);
	}

	@RequestMapping("onExportytd")
	public void onExportytd(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");

		String a = "E:/pos_tzrxx_export/p_p2p_pos_tzrxx";
		String b = ".sql";
		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dNow);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		dBefore = calendar.getTime(); // 得到前一天的时间

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); // 设置时间格式
		String defaultStartDate = sdf.format(dBefore); // 格式化前一天
		String defaultEndDate = sdf.format(dNow); // 格式化当前时间
		String filePath = a + defaultStartDate + b;// 拼接成文件路径
		String filename = "p_p2p_pos_tzrxx" + defaultStartDate + ".sql";
		System.out.println("前一天的时间是：" + defaultStartDate);
		System.out.println("生成的时间是：" + defaultEndDate);
		System.out.println("导出文件地址为: " + filePath);
		File d = new File(filePath);
		if (!d.exists()) {
			JOptionPane.showMessageDialog(null, "找不到下载文件!", "找不到下载文件!", JOptionPane.ERROR_MESSAGE);
		} else {
			// 设置文件MIME类型
			response.setContentType(request.getServletContext().getMimeType(filename));
			// 设置Content-Disposition
			response.setHeader("Content-Disposition", "attachment;filename=" + filename);
			// 读取目标文件，通过response将目标文件写到客户端
			// 获取目标文件的绝对路径
			// System.out.println(fullFileName);
			// 读取文件
			File file = new File(filePath);
			InputStream in = new FileInputStream(file);
			OutputStream out = response.getOutputStream();
			byte[] buffer = new byte[2048];
			// 写文件
			int len;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
			out.flush();
			in.close();
			out.close();

		}

	}

	@RequestMapping("onExport5")
	public void onExport5(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");


		String a = "E:/pos_tzrxx_export/p_p2p_pos_tzrxx";
		String b = ".sql";
		Date dBefore = new Date();
		Calendar c = Calendar.getInstance();
		// 减去一个星期
		c.add(Calendar.WEEK_OF_MONTH, -1);
		// 上个星期的星期天是1,所以要加上6
		c.set(Calendar.DAY_OF_WEEK, 6);
		dBefore = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); // 设置时间格式
		String defaultStartDate = sdf.format(dBefore);
		System.out.println("上周五日期是：" + defaultStartDate);
		String filePath = a + defaultStartDate + b;// 拼接成文件路径
		String filename = "p_p2p_pos_tzrxx" + defaultStartDate + ".sql";
		System.out.println("导出文件地址为: " + filePath);
		File d = new File(filePath);
		if (!d.exists()) {
			JOptionPane.showMessageDialog(null, "找不到下载文件!", "找不到下载文件!", JOptionPane.ERROR_MESSAGE);
		} else {
			// 设置文件MIME类型
			response.setContentType(request.getServletContext().getMimeType(filename));
			// 设置Content-Disposition
			response.setHeader("Content-Disposition", "attachment;filename=" + filename);
			// 读取目标文件，通过response将目标文件写到客户端
			// 获取目标文件的绝对路径
			// System.out.println(fullFileName);
			// 读取文件
			File file = new File(filePath);
			InputStream in = new FileInputStream(file);
			OutputStream out = response.getOutputStream();
			byte[] buffer = new byte[2048];
			// 写文件
			int len;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
			out.flush();
			in.close();
			out.close();

		}

	}

	@RequestMapping("onExport6")
	public void onExport6(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");


		String a = "E:/pos_tzrxx_export/p_p2p_pos_tzrxx";
		String b = ".sql";
		Date dBefore = new Date();
		Calendar c = Calendar.getInstance();
		// 减去一个星期
		c.add(Calendar.WEEK_OF_MONTH, -1);
		// 上个星期的星期天是1,所以要加上7
		c.set(Calendar.DAY_OF_WEEK, 7);
		dBefore = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); // 设置时间格式
		String defaultStartDate = sdf.format(dBefore);
		System.out.println("上周六日期是：" + defaultStartDate);
		String filePath = a + defaultStartDate + b;// 拼接成文件路径
		String filename = "p_p2p_pos_tzrxx" + defaultStartDate + ".sql";
		System.out.println("导出文件地址为: " + filePath);
		File d = new File(filePath);
		if (!d.exists()) {
			JOptionPane.showMessageDialog(null, "找不到下载文件!", "找不到下载文件!", JOptionPane.ERROR_MESSAGE);
		} else {
			// 设置文件MIME类型
			response.setContentType(request.getServletContext().getMimeType(filename));
			// 设置Content-Disposition
			response.setHeader("Content-Disposition", "attachment;filename=" + filename);
			// 读取目标文件，通过response将目标文件写到客户端
			// 获取目标文件的绝对路径
			// System.out.println(fullFileName);
			// 读取文件
			File file = new File(filePath);
			InputStream in = new FileInputStream(file);
			OutputStream out = response.getOutputStream();
			byte[] buffer = new byte[2048];
			// 写文件
			int len;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
			out.flush();
			in.close();
			out.close();

		}

	}

}
